package com.zhangxiaofanfan.web.controller;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 接收到请求之后将数据保存 kafka 上
 *
 * @author zhangxiaofanfan
 * @date 2024-07-02 18:56:39
 */
@RestController
public class ProducerController {
    @Autowired
    private KafkaTemplate<String, String> kafka;
    @Value("${spring.kafka.topic}")
    private String topic;

    @GetMapping("producer")
    public String producer(String msg) {
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, msg);
        kafka.send(record);
        return "Hello, " + msg;
    }
}
